package com.wx.wxpush.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 *@ClassName Tianqi
 *@Description TODO
 *@Author chen
 *@Date 2022/8/22 09:54
 */
public class Tianqi {
    private static String ak = "P5pDXKq7wpt7GKv0jnsi9puzWn2UqQfG";
    private static String district_id = "350206";
    public static JSONObject getTianqi() {
        String result = null;
        JSONObject today = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
                today = arr.getJSONObject(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }
    public static JSONObject getTianqiDist(String district_id) {
        String result = null;
        JSONObject today = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
                today = arr.getJSONObject(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }

    public static JSONObject getTomorrowTianqi() {
        String result = null;
        JSONObject tomorrow = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
                tomorrow = arr.getJSONObject(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tomorrow;
    }

    public static JSONObject getLocation(String district_id) {
        String result = null;
        JSONObject location = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONObject arr = jsonObject.getJSONObject("result").getJSONObject("location");
                location=arr;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }

    public static void main(String[] args) {
//        System.out.println(getTianqi());
        System.out.println(getLocation("350206"));
    }


}
